//
//  PayByBagViewController.m
//  XDCommonApp
//
//  Created by wanglong8889@126.com on 14-4-21.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "PayByBagViewController.h"
#import "XDHeader.h"
//#import "orderListViewController.h"
@interface PayByBagViewController ()

@end

@implementation PayByBagViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.titleLabel.text = @"支付宝支付";
    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"pd_25_bai"] forState:UIControlStateNormal];
    
    if (IOS7) {
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS7"];
    }else{
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS6"];
    }
    
    self.titleLabel.textColor = [UIColor whiteColor];
    UIWebView *malongWeb = [[UIWebView alloc] init];
    malongWeb.delegate = self;
    if (IOS7) {
        malongWeb.frame = CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height);
    }else{
        malongWeb.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
    [self.view addSubview:malongWeb];
    [malongWeb loadRequest:[NSURLRequest requestWithURL:self.webUrl]];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *webUrl = [NSString stringWithFormat:@"%@",[[webView request] URL]];
    
    BOOL isWeb = [webUrl rangeOfString:@"http://ts.leepet.com/alipay/call_back_url.php"].location !=NSNotFound;
    BOOL isSucess = [webUrl rangeOfString:@"success"].location !=NSNotFound;
    
    
    if (isWeb) {
        if (isSucess) {
            [self getState:webView.request.URL];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}


-(void)getState:(NSURL *) url{
    if ([XDTools NetworkReachable])
    {
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.view];
            

            NSString *tempic = mrequest.responseString;
            DDLOG(@"temdic = %@",tempic);
            
            if ([tempic isEqualToString:@"success"]) {
                
                [XDTools showTips:@"支付成功" toView:self.contentView];
                //跳到订单
//                [self completeShop];
            }else{
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
            if (error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];
            }
        }];
        
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}


//-(void)completeShop{
//    orderListViewController * ovc = [[orderListViewController alloc] init];
//    ovc.type = @"0";
//    [self.navigationController pushViewController:ovc animated:YES];
//}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
